﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FrontEndPlus
{
    public partial class wbfrm_Controles_Roles : System.Web.UI.Page
    {
        WCF_Gimnasio.cls_Rol _BSRoles = new WCF_Gimnasio.cls_Rol();
        WCF_Gimnasio.cls_Permisos_Roles _BSPermisos_Roles = new WCF_Gimnasio.cls_Permisos_Roles();
        WCF_Gimnasio.ServiceClient _servicio = new WCF_Gimnasio.ServiceClient();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _BSRoles.IdUsuarioInicioSeccion = Convert.ToInt32(Session["idUsuario"]);
                cb_Roles.DataTextField = "Nombre_Rol";
                cb_Roles.DataValueField = "Id_Rol";
                cb_Roles.DataSource = _servicio.Listar(1,_BSRoles).Tables[0];
                cb_Roles.DataBind();

                _BSPermisos_Roles.TipoListado = "ListadoFormularios";
                _BSPermisos_Roles.IdUsuarioInicioSeccion = Convert.ToInt32(Session["idUsuario"]);
                cb_Formularios.DataTextField = "Nombre_Formulario";
                cb_Formularios.DataValueField = "Id_Formulario";
                cb_Formularios.DataSource = _servicio.Listar(1,_BSPermisos_Roles).Tables[0];
                cb_Formularios.DataBind();
                llenarPermisos();
            }
        }

        //REGION DE METODOS
        #region METODOS

        //LLENAR PERMISOS POR USUARIO
        private void llenarPermisos()
        {
            try
            {
                if (cb_Roles.SelectedValue != null && cb_Formularios.SelectedValue != null)
                {
                    _BSPermisos_Roles.IdRol = Convert.ToInt32(cb_Roles.SelectedValue);
                    _BSPermisos_Roles.IdPagina = Convert.ToInt32(cb_Formularios.SelectedValue);
                    _BSPermisos_Roles.TipoListado = "ListadoControles";
                    DataTable Controles = _servicio.Listar(1, _BSPermisos_Roles).Tables[0];
                    _BSPermisos_Roles.TipoListado = "ControlesAutorizados";
                    DataTable autorizado = _servicio.Listar(1, _BSPermisos_Roles).Tables[0];
                    for (int i = 0; i < Controles.Rows.Count; i++)
                    {
                        DataRow[] foundRow = autorizado.Select("FK_Id_Control = " + "'" + Convert.ToInt32(Controles.Rows[i][4]) + "'");
                        if (foundRow.Length > 0)
                            Controles.Rows[i][0] = true;
                    }
                    dtg_Controles_Rol.DataSource = Controles;
                    dtg_Controles_Rol.DataBind();
                    if (Convert.ToInt32(cb_Roles.SelectedValue) != 1)
                    {
                        btn_Actualizar.Enabled = true;
                        dtg_Controles_Rol.Enabled = true;
                    }
                    else
                    {
                        dtg_Controles_Rol.Enabled = false;
                        btn_Actualizar.Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
                string script = @"<script type='text/javascript'> smoke.alert('" + ex.Message + "', function (e) {}, {ok: 'OK',classname: 'custom-class'}); </script>";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false);
            }
            finally
            {
                _servicio.Close();
            }
        }
        #endregion

        protected void dtg_Controles_Rol_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[4].Visible = false;
        }

        protected void cb_Roles_SelectedIndexChanged(object sender, EventArgs e)
        {
            llenarPermisos();
        }

        protected void btn_Actualizar_Click(object sender, EventArgs e)
        {
            try
            {
                string nombreRol = cb_Roles.Text;
                _BSPermisos_Roles.IdRol = Convert.ToInt32(cb_Roles.SelectedValue);
                _BSPermisos_Roles.IdPagina = Convert.ToInt32(cb_Formularios.SelectedValue);
                string referencia = "";
                _servicio.EliminarRegistro(ref referencia, _BSPermisos_Roles);
                foreach (GridViewRow row in dtg_Controles_Rol.Rows)
                {
                    CheckBox check = row.FindControl("Selec") as CheckBox;
                    if (check.Checked)
                    {
                        _BSPermisos_Roles.IdControl = Convert.ToInt32(row.Cells[4].Text);
                        _servicio.InsertarActualizarRegistro(true, ref referencia, _BSPermisos_Roles);
                    }
                }

                if (nombreRol != Session["nombreRol"].ToString())
                {
                    string script = @"<script type='text/javascript'> smoke.alert('Permisos Actualizados Correctamente', function (e) {}, {ok: 'OK',classname: 'custom-class'}); </script>";
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false);
                }
                else
                {
                    FormsAuthentication.SignOut();
                    FormsAuthentication.RedirectToLoginPage();
                }
            }
            catch (Exception ex)
            {
                string script = @"<script type='text/javascript'> smoke.alert('" + ex.Message + "', function (e) {}, {ok: 'OK',classname: 'custom-class'}); </script>";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false);
            }
            finally
            {
                _servicio.Close();
            }
        }
    }
}